package app.netant.badbook.haxwen;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import app.netant.NetAntBase;
/**
 * HaxWen明细解析
 * @author JiaJ
 *
 */
public class HaxwenNetDetail extends NetAntBase {
	
	private HaxwenEntity entity;

	public HaxwenNetDetail(String netSite) {
		super(netSite);
	}

	public void setEntity(HaxwenEntity entity){
		this.entity = entity;
	}
	
	public void analyseTitle(){
		
		Elements activePart = document.select("#container #content .inner .details .book-info .book-title");
		if(activePart.size()>0){
			Element activeNode = activePart.get(0);
			String title = this.getString(activeNode.select("h1").text());
			String writer = this.getString(activeNode.select("em").text());
			int idx = writer.indexOf("：");
			if(idx > 0){
				writer = writer.substring(idx+1);
			}
			
			this.entity.setNovelName(title);
			this.entity.setNovelWriter(writer);
			this.entity.setNovelFullName(title);
			/*System.out.println(title);
			System.out.println(writer);*/
		}
		
	}
	
	public void analyseTags(){
		
		Elements activePart = document.select("#container #content .inner .details .book-info .book-stats");
		if(activePart.size()>0){
			Element activeNode = activePart.get(0);
			String tags = activeNode.text();
			
			String[] tagSp = tags.split("：");
			
			List<String> names = new ArrayList<String>();
			List<String> values = new ArrayList<String>();
			for(int i=0;i<tagSp.length;i++){
				
				String activeVal = tagSp[i].trim();
				if(i == 0){
					names.add(activeVal);
				}else if(i==tagSp.length-1){
					values.add(activeVal);
				}else{
					
					int fstPoint = activeVal.indexOf(" ");
					if(fstPoint > 0){
						
						String oVal = activeVal.substring(0,fstPoint);
						String oNam = activeVal.substring(fstPoint+1);
						values.add(oVal);
						names.add(oNam);
						
					}
					
				}
				
			}
			
			if(names.size() == values.size() && names.size()>0){
				
				for(int i=0;i<names.size();i++){
					
					String tagName = names.get(i);
					String tagValue = values.get(i);
					
					this.entity.setTag(tagName, tagValue);
					
					/*System.out.println(tagName+"="+tagValue);*/
					
				}
				
			}
			
			
		}
		
	}
	
	public void analyseHref(){
		
		Elements activePart = document.select("#container #content .inner .details .book-info .book-link a");
		if(activePart.size()>0){
			
			for(int i=0;i<activePart.size();i++){
				
				String txt = activePart.get(i).text().trim();
				if("TXT下载".equals(txt)){
					
					String href = activePart.get(i).attr("href");
					if(href.startsWith("/")){
						href = "https://www.haxwx11.com"+href;
					}
					
					this.entity.setDownPath(href);
					
					/*System.out.println(href);*/
					
					break;
					
				}
				
			}
			
		}
		
	}
	
	public void analyseMemo(){
		
		Elements activePart = document.select("#container #content .inner .details .book-info .book-intro");
		if(activePart.size()>0){
			Element activeNode = activePart.get(0);
			String memo = this.getString(activeNode.text());
			
			this.entity.setNovelMemo(memo);
			
			/*System.out.println(memo);*/
			
		}
		
	}
	
	public void analyseType(){
		
		Elements activePart = document.select("#wrapper .crumbs a");
		if(activePart.size()>2){
			Element activeNode = activePart.get(1);
			String type = this.getString(activeNode.text());
			
			this.entity.setNovelType(type);
			
			/*System.out.println(type);*/
		}
		
	}
	
	public void analyse(){
		
		this.analyseTitle();
		this.analyseTags();
		this.analyseMemo();
		this.analyseHref();
		this.analyseType();
		
		this.entity.download();
		this.entity.insert();
		
	}
	
	
	public static void main(String[] args) {
		
		HaxwenNetDetail detail = new HaxwenNetDetail("https://www.haxwx11.com/files/article/info/72/72215.htm");
		detail.analyseTitle();
		detail.analyseTags();
		detail.analyseMemo();
		detail.analyseHref();
		detail.analyseType();
		
	}
}
